package cn.rubintry.bug.hunting.fragment

import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.toBitmap
import androidx.fragment.app.viewModels
import androidx.viewbinding.ViewBinding
import cn.rubintry.animate.AnimateUtils
import cn.rubintry.bug.common.base.BaseFragment
import cn.rubintry.bug.hunting.R
import cn.rubintry.bug.hunting.activity.HistoryActivity
import cn.rubintry.bug.hunting.activity.LoginActivity
import cn.rubintry.bug.hunting.activity.MyLogActivity
import cn.rubintry.bug.hunting.activity.SettingActivity
import cn.rubintry.bug.hunting.databinding.FragmentElasticMineBinding
import cn.rubintry.bug.hunting.common.event.LoginEvent
import cn.rubintry.bug.hunting.common.event.LogoutEvent
import cn.rubintry.bug.hunting.common.http.viewmodel.MineViewModel
import cn.rubintry.bug.hunting.common.manager.UserManager

import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.util.SmartGlideImageLoader
import jp.wasabeef.glide.transformations.BlurTransformation
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import kotlin.math.abs


/**
 * 我的
 */
class MineFragment() : BaseFragment<FragmentElasticMineBinding>(), View.OnClickListener {

    private val mineViewModel : MineViewModel by viewModels<MineViewModel>()


    override fun layoutId(): Int {
        return R.layout.fragment_elastic_mine
    }


    override fun initView() {
        if(!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this)
        }
        val drawable = ContextCompat.getDrawable(requireContext(), R.drawable.bg_mine)
        Glide.with(this)
            .load(drawable)
            .transform(BlurTransformation(25, 1))
            .into(binding?.imgMineBg!!)

        binding?.nbElasticView?.setHeader(binding?.mEmptyHead)
        binding?.nbElasticView?.setOnReadyPullListener { binding?.nslMine?.scaleY == 1.0f }

        binding?.clLoginClickArea?.setOnClickListener(this)
        binding?.llHistory?.setOnClickListener(this)
        binding?.llSetting?.setOnClickListener(this)
        binding?.imgMineHeader?.setOnClickListener(this)
        binding?.llMyLog?.setOnClickListener(this)
    }


    override fun observeData() {

    }

    override fun bindingClazz(): Class<out ViewBinding> {
        return FragmentElasticMineBinding::class.java
    }

    override fun onNetWorkError(msg: String) {

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mineViewModel.getHomeBackgroundImages()
        renderDataToView()
    }




    override fun onDestroy() {
        super.onDestroy()
        if(EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this)
        }
    }

    override fun onClick(v: View?) {
        when(v){
            binding?.clLoginClickArea -> {
                if(!UserManager.getInstance().isLogin()){
                    startActivity(Intent(requireContext(), LoginActivity::class.java))
                }
            }
            binding?.llHistory -> {
                if(!UserManager.getInstance().isLogin()){
                    startActivity(Intent(requireContext(), LoginActivity::class.java))
                    return
                }
                startActivity(Intent(requireContext() , HistoryActivity::class.java))
            }

            binding?.llSetting -> {
                startActivity(Intent(requireContext() , SettingActivity::class.java))
            }

            binding?.imgMineHeader -> {
                if(!UserManager.getInstance().isLogin()){
                    startActivity(Intent(requireContext(), LoginActivity::class.java))
                    return
                }
                binding?.imgMineHeader?.let {
                    XPopup.Builder(requireContext())
                        .asImageViewer(it , UserManager.getInstance().getUserData()?.detail?.headerUrl ?: "" , SmartGlideImageLoader())
                        .isShowSaveButton(false)
                        .show()
                }
            }

            binding?.llMyLog -> {
                startActivity(Intent(requireContext() , MyLogActivity::class.java))
            }
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun loginSuccess(event: LoginEvent){
        renderDataToView()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun logout(event: LogoutEvent){
        renderDataToView()
    }

    /**
     * 将数据渲染到视图
     */
    private fun renderDataToView() {
        UserManager.getInstance().getUserData()?.let { data ->
            data.detail?.headerUrl?.let { url ->
                Glide.with(this@MineFragment)
                    .load(url)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .into(binding?.imgMineHeader!!)
            }
            binding?.tvUserName?.text = data.detail.userName
//            binding?.tvUserSign?.text = data.detail.userSign
            return
        }
        binding?.tvUserName?.text = "立即登录"
//        binding?.tvUserSign?.text = "Bug猎场 代码领航"
        Glide.with(this@MineFragment)
            .load(R.mipmap.mlogo)
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
            .into(binding?.imgMineHeader!!)
    }

}